package com.dsteiner.gwt.cafeteriasophia.client.controller;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import com.dsteiner.gwt.cafeteriasophia.client.domain.Product;
import com.dsteiner.gwt.cafeteriasophia.client.domain.Purchase;
import com.dsteiner.gwt.cafeteriasophia.client.model.PurchaseModel;
import com.dsteiner.widgets.client.JsUtil;
import com.dsteiner.widgets.client.ObjectUtil;
import com.dsteiner.widgets.client.Widgets.ILazyWidget;
import com.dsteiner.widgets.client.Widgets.IWidget;
import com.dsteiner.widgets.client.Widgets.WidgetListenerAdapter;
import com.dsteiner.widgets.client.model.PrefilledTextBoxModel;

public class PurchaseController {

	private PurchaseModel purchaseModel;
	private IWidget pricePanel;
	private PrefilledTextBoxModel priceTextBoxModel;
	private PrefilledTextBoxModel amountTextBoxModel;

	public void registerPurchaseModel(PurchaseModel purchaseModel) {
		this.purchaseModel = purchaseModel;
		purchaseModel.addPropertyChangeListener("purchase", new PropertyChangeListener(){
			public void propertyChange(PropertyChangeEvent evt) {
				updatePurchase();
			}
		});
	}

	public void registerPurchasePricePanel(IWidget pricePanel) {
		this.pricePanel = pricePanel;
	}

	public void registerPurchasePriceTextBoxModel(final PrefilledTextBoxModel priceTextBoxModel) {
		this.priceTextBoxModel = priceTextBoxModel;
		priceTextBoxModel.addPropertyChangeListener("hasFocus", new PropertyChangeListener(){
			public void propertyChange(PropertyChangeEvent evt) {
				if(!priceTextBoxModel.hasFocus()){
					String text = priceTextBoxModel.getText();
					if(priceTextBoxModel.isEdited() && !ObjectUtil.isDouble(text)){
						JsUtil.getInstance().showAlert("ingrese un numero (por ejemplo 4.5)");
						priceTextBoxModel.setText(null);
						return;
					}
					Purchase purchase = purchaseModel.getPurchase();
					purchase.setPrice(priceTextBoxModel.isEdited() ? new Double(text) : 0);
				}
			}
		});
	}

	public void registerPurchasesTextBoxModel(final PrefilledTextBoxModel amountTextBoxModel) {
		this.amountTextBoxModel = amountTextBoxModel;
		amountTextBoxModel.addPropertyChangeListener("hasFocus", new PropertyChangeListener(){
			public void propertyChange(PropertyChangeEvent evt) {
				if(!amountTextBoxModel.hasFocus()){
					String text = amountTextBoxModel.getText();
					if(amountTextBoxModel.isEdited() && !ObjectUtil.isDouble(text)){
						JsUtil.getInstance().showAlert("ingrese un numero (por ejemplo 4.5)");
						amountTextBoxModel.setText(null);
						return;
					}
					Purchase purchase = purchaseModel.getPurchase();
					purchase.setAmount(amountTextBoxModel.isEdited() ? new Double(text) : 0);
				}
			}
		});
	}

	public void registerPurchaseWidget(ILazyWidget purchaseWidget) {
		purchaseWidget.addWidgetListener(new WidgetListenerAdapter() {
			@Override
			public void onAttach(IWidget widget) {
				updatePurchase();
			}
		});
	}

	private void updatePurchase() {
		Purchase purchase = purchaseModel.getPurchase();
		amountTextBoxModel.setText(purchase.getAmount() > 0 ? purchase.getAmount()+"" : null);
		priceTextBoxModel.setText(purchase.getPrice() > 0 ? purchase.getPrice()+"" : null);
		
		Product product = purchase.getProduct();
		Double cost = product.getCost();
		pricePanel.setVisible(cost != null);
		priceTextBoxModel.setText(cost != null ? cost.toString() : null);
	}
}